package work.soho.proxy;

import org.ini4j.Ini;

import java.io.File;
import java.io.IOException;

public class ConfigManage {
    private Ini iniFile;
    private String iniFilePath = System.getProperty("user.home") + File.separator +"proxy.ini";
    private PartsConfigure partsConfigure;

    private String defaultSection = "default";

    /**
     * 检查配置文件是否存在
     *
     * @return
     */
    public boolean isExists() {
        System.out.println(iniFilePath);
        File file = new File(iniFilePath);
        return file.exists();
    }

    /**
     * 从配置文件获取值
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        if(iniFile != null) {
            return iniFile.get(defaultSection).get(key);
        } else {
            return null;
        }
    }

    /**
     * 获取boolean
     *
     * @param key
     * @return
     */
    public boolean getBoolValue(String key) {
        String val = getValue(key);
        if (val != null && val.equals("true")) {
            return true;
        }
        return false;
    }

    /**
     * 获取值
     *
     * 带默认值返回
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public String getValue(String key, String defaultValue) {
        String value = getValue(key);
        if(value == null) return defaultValue;
        return value;
    }

    /**
     * 获取PartsConfigure
     *
     * @return
     */
    public PartsConfigure getPartsConfigure() throws IOException {
        if(partsConfigure == null) {
            partsConfigure = new PartsConfigure();
            reloadPartsConfigure();
        }
        return partsConfigure;
    }

    /**
     * 重新配置
     *
     * @throws IOException
     */
    public void reloadPartsConfigure() throws IOException {
        if(!isExists()) return;
        File file = new File(iniFilePath);
        Ini ini = new Ini();
        if(file.exists()) {
            ini.load(file);
        }
        iniFile = ini;
        partsConfigure.setAccessSecret(getValue("accessSecret"));
        partsConfigure.setAccessKeyId(getValue("accessKeyId"));
        partsConfigure.setProxyPort(getValue("port"));
        partsConfigure.setAutoCheckTs(new Integer(getValue("autoCheckTs")==null ? (String) "0" : getValue("autoCheckTs")));
        partsConfigure.setBaseDomain(getValue("baseDomain"));
        partsConfigure.setUseSocket5(getBoolValue("useSocket5"));
        partsConfigure.setUseDdns(getBoolValue("useDdns"));
        partsConfigure.setKeyPairName(getValue("keyPairName"));
        //
    }

    /**
     * 保存配置信息到本地
     *
     * @param partsConfigure
     */
    public void save2File(PartsConfigure partsConfigure) throws IOException {
        File file = new File(iniFilePath);
        Ini ini = new Ini();
        if(file.exists()) {
            file.delete();
        }
        ini.add(defaultSection, "accessSecret", partsConfigure.getAccessSecret());
        ini.add(defaultSection, "accessKeyId", partsConfigure.getAccessKeyId());
        ini.add(defaultSection, "port", partsConfigure.getProxyPort());
        ini.add(defaultSection, "autoCheckTs", partsConfigure.getAutoCheckTs());
        ini.add(defaultSection, "baseDomain", partsConfigure.getBaseDomain());
        ini.add(defaultSection, "useSocket5", partsConfigure.getUseSocket5());
        ini.add(defaultSection, "useDdns", partsConfigure.getUseDdns());
        ini.add(defaultSection, "keyPairName", partsConfigure.getKeyPairName());
        ini.store(file);
    }
}
